<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller\finance;

/**
 * Description of RechargeController
 *
 * @author SoChishun
 */
class RechargeController extends \app\index\controller\BaseController {

    public function rechargeAction($type = '') {
        $paychannels = ['recharge', 'wiipayh5', 'wiipaywx'];
        $rndchannel = ($type && in_array($type, $paychannels)) ? $type : $paychannels[0];
        return view($rndchannel);
    }

    public function doRechargeAction() {
        $request = \think\Request::instance();
        $request->only(['amount', 'token']);
        $data = $request->post();
        $recharge = new \app\index\model\finance\RechargeModel();
        $result = $recharge->saveRecharge($data, $this->loginInfo['id'], 'cz', '');
        if (!$result['success']) {
            $this->error($result['message']);
        }
        $out_trade_no = $result['message']['out_trade_no'];
        redirect('rechargeNotify', 'out_trade_no=' . $out_trade_no)->send();
    }

    public function rechargeNotifyAction() {
        $request = \think\Request::instance();
        $out_trade_no = $request->param('out_trade_no');
        $recharge = new \app\index\model\finance\RechargeModel();
        $result = $recharge->rechargeConfirm($out_trade_no, $this->loginInfo['id']);
        if (!$result['success']) {
            exit($result['message']);
        }
        $this->success('充值成功!', url('history'));
    }

    public function historyAction() {
        $where['user_id'] = $this->loginInfo['id'];
        $list = \app\index\model\finance\RechargeModel::where($where)->order('id desc')->paginate(10);
        return view('', ['list' => $list]);
    }

}
